\documentclass[a4paper,12pt]{article}

\usepackage[margin=1.5cm]{geometry}
\usepackage{listings}
\usepackage{enumitem}

\title{Vim Experience}
\author{Chen Rushan\\chenrsster@gmail.com}
\date{2009.11.18 23:38}

\setlength{\parskip}{1.2ex}
\setlength{\parindent}{0pt}

\lstset{
    frame=single, framesep=0pt, framexleftmargin=-1pt, framextopmargin=3pt,
    framexbottommargin=3pt, aboveskip=0.5cm, belowskip=0.2cm, columns=flexible,
    basicstyle=\ttfamily
}

\begin{document}

\maketitle

\section{encoding, fileencoding, fileencodings}

    \begin{itemize}[leftmargin=3ex]
        \item \textbf{encoding}: Sets the character encoding used inside Vim. It 
            applies to text in the buffers, registers, Strings in expressions,
            text stored in the viminfo file, etc.  It sets the kind of
            characters which Vim can work with.

            And this is the default encoding used when you start editing a new
            file.

        \item \textbf{fileencodings}: this contains a list of encodings that
            will be considered when opening an existing file. When a file is
            read, Vim tries to use the first mentioned character encoding.  If
            an error is detected, the next one in the list is tried.  When an
            encoding is found that works, \textbf{fileencoding} is set to it.
            If all fail, \textbf{fileencoding} is set to the value of 
            \textbf{encoding}.

        \item \textbf{fileencoding}: \textbf{set fileencoding} only affects
            local buffer, it changes the encoding of current buffer, and if it's
            not the same as \textbf{encoding}, conversion will be done when
            reading and writing the file.

            I think when reading, the conversion is \textbf{fileencoding}
            $\longrightarrow$ \textbf{encoding}, and when writing, the
            conversion is \textbf{encoding} $\longrightarrow$
            \textbf{fileencoding}. Since urxvt is only capable of displaying
            utf-8 characters, so if you want to correctly display a gb file, you
            need to set \textbf{fileencoding} = gb2312 and \textbf{encoding} =
            utf-8, this will cause the file to be converted to utf-8 when
            reading into the buffer. But if you have \textbf{encoding} = gb2312,
            things don't work, cuz gb character can't be displayed correctly
            under urxvt.

    \end{itemize}

    So \textbf{fileencoding} is the one that decides the encoding of the file on
    disk, \textbf{encoding} just decides characters used inside vim.

\end{document}

